package cn.pugle.base.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * <p> Created by tzp on 2017/11/7.
 */
public class LockTest {
    private ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    private List<String> data = new ArrayList<>();

    public void read() {
        readWriteLock.readLock().lock();
        System.out.println("reading " + data.size());
        readWriteLock.readLock().unlock();
    }

    public void write() {
        readWriteLock.writeLock().lock();
        System.out.println("writing");
        data.add("a");
        readWriteLock.writeLock().unlock();
    }

    public static void main(String[] args) {
        LockTest lockTest = new LockTest();
    }
}
